//
//  HourseDetailHeadView.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/6.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "HourseDetailHeadView.h"
#import "UIImageView+WebCache.h"
#import "config.h"
#import "SingletonData.h"
#import "FTWCache.h"
#import "UIButton+WebCache.h"
#import "NSString+StringHight.h"
#import "UserModal.h"
#import "StringTool.h"
#import "UserModal.h"
#import "Tools.h"
#import "PhotoShowManage.h"
#import "CustomMoviePlayerViewController.h"
#import "BaseNvc.h"
#import "IWNavigationController.h"
#import "ImagePlayerView.h"




@interface HourseDetailHeadView ()<MWPhotoBrowserDelegate,ImagePlayerViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *dayLab;
@property (weak, nonatomic) IBOutlet UILabel *hourLab;
@property (weak, nonatomic) IBOutlet UILabel *mintuLab;
@property (weak, nonatomic) IBOutlet UILabel *seconLab;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UILabel *openTimeLab;
@property (weak, nonatomic) IBOutlet UILabel *dolLab;
@property (weak, nonatomic) IBOutlet UILabel *rmbLab;
@property (weak, nonatomic) IBOutlet UILabel *finishLab;
@property (weak, nonatomic) IBOutlet UIView *clockView;

@property (weak, nonatomic) IBOutlet UILabel *maxRateTitleLab;
@property (weak, nonatomic) IBOutlet UIView *maxRateLine;

@property (weak, nonatomic) IBOutlet UILabel *maxRateLab;
@property (weak, nonatomic) IBOutlet UILabel *hourseCodeLab;
@property (weak, nonatomic) IBOutlet UILabel *openTimeLab2;
@property (weak, nonatomic) IBOutlet UILabel *areaLab;

@property (weak, nonatomic) IBOutlet UILabel *baoZhuLab;

@property (weak, nonatomic) IBOutlet UILabel *huiGouLab;
@property (weak, nonatomic) IBOutlet UILabel *rentLab;
@property (weak, nonatomic) IBOutlet UILabel *rentRateLab;
@property (weak, nonatomic) IBOutlet UILabel *allCountLab;
@property (weak, nonatomic) IBOutlet UILabel *saledCountLab;

@property (weak, nonatomic) IBOutlet UILabel *progressLab1;
@property (weak, nonatomic) IBOutlet UILabel *progressLab2;
@property (weak, nonatomic) IBOutlet UILabel *progressLab3;
@property (weak, nonatomic) IBOutlet UILabel *progressLab4;
@property (weak, nonatomic) IBOutlet UIImageView *progressImgView;



@property (weak, nonatomic) IBOutlet UILabel *huiDetailLAB;

@property (weak, nonatomic) IBOutlet UILabel *yongDetailLab;
@property (weak, nonatomic) IBOutlet UILabel *yongRedDetailLab;

@property (weak, nonatomic) IBOutlet UILabel *maiDetailLab;


@property (copy,nonatomic) NSString *openData,*expiryDate;
@property (weak, nonatomic) IBOutlet UIView *huiViewLay;
@property (weak, nonatomic) IBOutlet UIView *yongViewLay;
@property (weak, nonatomic) IBOutlet UIView *maiViewLay;

@property (strong, nonatomic) HourseDetailData *headData;
@property (strong, nonatomic) UINavigationController *bufNvc;
@property (strong, nonatomic) ImagePlayerView *showImgView;
@property (nonatomic,strong)NSTimer *timer;

@end


@implementation HourseDetailHeadView
@synthesize openData,headData,showImgView,timer;

+(HourseDetailHeadView*)getCustomView{

    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"HourseDetailHeadView" owner:nil options:nil];
    HourseDetailHeadView *headView = [nibView objectAtIndex:0];
    

    [headView initView];
    
    
    return headView;
}


-(void)initView{
    
    self.huiDetailLAB.preferredMaxLayoutWidth = PMW-10-35-10-10;
    self.maiDetailLab.preferredMaxLayoutWidth = PMW-10-35-10-10;
    self.yongDetailLab.preferredMaxLayoutWidth = PMW-10-35-10-10;
    self.yongRedDetailLab.preferredMaxLayoutWidth = PMW-10-35-10-10;
    self.addressLab.preferredMaxLayoutWidth = PMW-10-12-8-10;
    
    
    [self.huiDetailLAB layoutIfNeeded];
    [self.yongDetailLab layoutIfNeeded];
    [self.yongRedDetailLab layoutIfNeeded];
    [self.maiDetailLab layoutIfNeeded];
    [self.addressLab layoutIfNeeded];
    
    [self.dayLab.layer setMasksToBounds:YES];
    [self.dayLab.layer setCornerRadius:2];
    [self.hourLab.layer setMasksToBounds:YES];
    [self.hourLab.layer setCornerRadius:2];
    [self.mintuLab.layer setMasksToBounds:YES];
    [self.mintuLab.layer setCornerRadius:2];
    [self.seconLab.layer setMasksToBounds:YES];
    [self.seconLab.layer setCornerRadius:2];
    
    self.finishLab.layer.masksToBounds = YES;
    self.finishLab.layer.cornerRadius = 1;
    
    
    
    if([UserModal shareUserModel].userType == UserTypeCustomer){
    
        [self.downloadBtn1 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0x999999)] forState:UIControlStateNormal];
        [self.downloadBtn1 setBackgroundColor:UIColorFromRGB(0x999999)];
        [self.downloadBtn1 setUserInteractionEnabled:false];
        
        UIView *finishLabSup = self.finishLab.superview;
        for (UIView *sub in finishLabSup.subviews) {
            [sub removeFromSuperview];
        }
        
        for (UIView *sub in self.huiViewLay.subviews) {
            [sub removeFromSuperview];
        }
        
        for (UIView *sub in self.yongViewLay.subviews) {
            [sub removeFromSuperview];
        }
        
        
        [self restHight:finishLabSup];
        [self restHight:self.huiViewLay];
        [self restHight:self.yongViewLay];
        
        
        [finishLabSup setNeedsLayout];
        [finishLabSup  layoutIfNeeded];
        
    }else if ([UserModal shareUserModel].userType == UserTypeMiddleMan){
        
        for (UIView *sub in self.yongViewLay.subviews) {
            [sub removeFromSuperview];
        }
        
        [self restHight:self.yongViewLay];
    }
    
    
    
    if ([UserModal shareUserModel].userType==UserTypeCustomer||[UserModal shareUserModel].userType==UserTypeMiddleMan) {
        
        UIView *timeView = self.finishLab.superview;
        
        for (UIView  *view in timeView.subviews) {
            [view removeFromSuperview];
        }
        
        for (NSLayoutConstraint *constraint in timeView.constraints) {
            if (constraint.firstAttribute == NSLayoutAttributeHeight && constraint.firstItem == timeView) {
                constraint.constant = 0;
                [timeView layoutIfNeeded];
                break;
            }
        }
        
        
        UIView *superView = timeView.superview;
        for (NSLayoutConstraint *constraint in superView.constraints) {
            if (constraint.firstAttribute == NSLayoutAttributeHeight && constraint.firstItem == superView) {
                constraint.constant = 340-37;
                [superView layoutIfNeeded];
                break;
            }
        }
        
        
    }
    
    
    self.showImgView = [[ImagePlayerView alloc]initWithFrame:CGRectMake(0, 0, PMW, self.mainImgBtn.frame.size.height)];
    [self.mainImgBtn addSubview:self.showImgView];
    
    

    [self.huiViewLay setNeedsLayout];
    [self.yongViewLay setNeedsLayout];
    [self.maiViewLay setNeedsLayout];
    [self setNeedsLayout];
    
    [self.huiViewLay layoutIfNeeded];
    [self.yongViewLay layoutIfNeeded];
    [self.maiViewLay layoutIfNeeded];
    [self layoutIfNeeded];

}



-(void)addTimer;
{
    if ([UserModal shareUserModel].userType==UserTypeCustomer) {
        
        [self.clockView setHidden:YES];
        [self.finishLab setHidden:YES];
        
    }else{
        
        [self.clockView setHidden:false];
        [self.finishLab setHidden:false];
        
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    }
}


-(void)restHight:(UIView *)view{
    
    NSLayoutConstraint *dd = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
    [view addConstraint:dd];
}



-(void)setData:(HourseDetailData*)data nvc:(UINavigationController *)nvc{
   
    
    self.bufNvc = nvc;
    
    
    [self.mainImgBtn setBackgroundColor:NoImgColor];
//    [self.mainImgBtn sd_setImageWithURL:[NSURL URLWithString:data.mainImgUrl] forState:UIControlStateNormal];
    
    headData = data;
    
     [self.showImgView initWithCount:data.photoShowImgArray.count delegate:self];
  
    


    [self.titleLab setText:data.title];
    [self.addressLab  setText:data.address];
    [self.openTimeLab setText:[NSString stringWithFormat:@"开盘时间:%@",data.openTime]];

    
    [self.dolLab setText:[NSString stringWithFormat:@"$%@",data.dol]];
    [self.rmbLab setText:[NSString stringWithFormat:@"约¥%@万",[StringTool numberForm:data.rmb]]];
    [self.maxRateLab setText:data.maxRate];
    [self.hourseCodeLab setText:data.hourseCode];
    [self.openTimeLab2 setText:data.openTime];
    
    [self.areaLab setText:data.area];
    [self.baoZhuLab setText:data.baoZhu];
    [self.huiGouLab setText:data.huiGou];
    [self.rentLab setText:data.rent];
    [self.rentRateLab setText:data.rentRate];
    
    [self.allCountLab setText:data.allCount];
    [self.saledCountLab setText:data.saledCount];
    
    switch (data.progressState) {
        case 1:
            
            [self.progressLab1 setTextColor:[UIColor redColor]];
            [self.progressLab2 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressLab3 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressLab4 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressImgView setImage:[UIImage imageNamed:@"xiaoshou_zhuantai_b"]];
            
            
            break;
            
        case 2:
            
            [self.progressLab1 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab2 setTextColor:[UIColor redColor]];
            [self.progressLab3 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressLab4 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressImgView setImage:[UIImage imageNamed:@"xiaoshou_zhuantai_c"]];
            
            break;
            
        case 3:
            [self.progressLab1 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab2 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab3 setTextColor:[UIColor redColor]];
            [self.progressLab4 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressImgView setImage:[UIImage imageNamed:@"xiaoshou_zhuantai_d"]];
            
            
            break;
            
        case 4:
            
            [self.progressLab1 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab2 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab3 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab4 setTextColor:[UIColor redColor]];
            [self.progressImgView setImage:[UIImage imageNamed:@"xiaoshou_zhuantai_e"]];
            break;
            
            
        default:
            break;
    }
    
    
    
    if(self.huiDetailLAB!=nil&&self.huiDetailLAB.superview!=nil){
        

       data.huiDetail =  [data.huiDetail stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
        [self.huiDetailLAB setAttributedText:[data.huiDetail attributedStringFromStingWithFont:self.huiDetailLAB.font withLineSpacing:3]];
    }
    
    if(self.yongDetailLab!=nil&&self.yongDetailLab.superview!=nil){
        
       data.yongDetail = [data.yongDetail stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
        [self.yongDetailLab setAttributedText:[data.yongDetail attributedStringFromStingWithFont:self.yongDetailLab.font withLineSpacing:3]];
    }
    
    if(self.yongRedDetailLab!=nil&&self.yongRedDetailLab.superview!=nil){
        
       data.yongRedDetail = [data.yongRedDetail stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
        [self.yongRedDetailLab setAttributedText:[data.yongRedDetail attributedStringFromStingWithFont:self.yongRedDetailLab.font withLineSpacing:3]];
    }
    
    if(self.maiDetailLab!=nil&&self.maiDetailLab.superview!=nil){
        
       data.maiDetail = [data.maiDetail stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
        [self.maiDetailLab setAttributedText:[data.maiDetail attributedStringFromStingWithFont:self.maiDetailLab.font withLineSpacing:3]];
    }
    
    

    
    if ([UserModal shareUserModel].userType == UserTypeCustomer || [UserModal shareUserModel].userType == UserTypeMiddleMan) {
        self.maxRateLab.hidden = YES;
        self.maxRateLine.hidden = YES;
        self.maxRateTitleLab.hidden = YES;
    }
    else
    {
        self.maxRateLab.hidden = NO;
        self.maxRateLine.hidden = NO;
        self.maxRateTitleLab.hidden = NO;
    }
    
    
    openData = data.openTimeWithHour;
    self.expiryDate = data.expiryDate;
    
    [self addTimer];
    [self layoutIfNeeded];
    
   
}



-(void)btnOnClick:(UIButton*)sender{

    if(headData.photoShowImgArray.count>0){
        MWPhotoBrowser *br = [PhotoShowManage  getMWPhotoShowInstance:self];
        [br setCurrentPhotoIndex:sender.tag];
        [self.bufNvc pushViewController:br animated:YES];
    }
}





/**
 *  倒计时
 */
-(void)timeFireMethod
{
    //2014-04-01 12:00:00
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    //    openData = @"2015-05-06 10:46:00";
    
    NSDate *fireDate=[dateFormatter dateFromString:self.expiryDate];
    fireDate = [FTWCache getLocData:fireDate];
    
    NSDate *today = [NSDate date];//当前时间
    today = [FTWCache getLocData:today];
    
    
    unsigned int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *d = [calendar components:unitFlags fromDate:today toDate:fireDate options:0];//计算时间差
    
    
    NSInteger day = [d day];
    NSInteger hour = [d hour];
    NSInteger minute = [d minute];
    NSInteger second = [d second];
    
    self.dayLab.text = [NSString stringWithFormat:@"%.2ld ",(long)day];
    self.hourLab.text = [NSString stringWithFormat:@"%.2ld",(long)hour];
    self.mintuLab.text = [NSString stringWithFormat:@"%.2ld",(long)minute];
    self.seconLab.text = [NSString stringWithFormat:@"%.2ld",(long)second];
    

    if (second<0||minute<0||hour<0||day<0) {
        [self.finishLab setHidden:NO];
        [self.clockView setHidden:YES];
    }else{
        [self.finishLab setHidden:YES];
        [self.clockView setHidden:NO];
    }
}





#pragma mark scrollView的代理

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index{
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:[headData.photoShowImgArray objectAtIndex:index]] placeholderImage:[UIImage imageNamed:DEFAULT_IMG]];
}



- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView didTapAtIndex:(NSInteger)index{
    
    
    if(headData.photoShowImgArray.count>0){
        MWPhotoBrowser *br = [PhotoShowManage  getMWPhotoShowInstance:self];
        [br setCurrentPhotoIndex:index];
        [self.bufNvc pushViewController:br animated:YES];
    }
    
}



#pragma MWPhotoShowDelegate
#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return headData.photoShowImgArray.count;
}


- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
    
    
    //   return  [MWPhoto photoWithURL:[NSURL URLWithString:headData.mainImgUrl]];
    
    if (index < headData.photoShowImgArray.count)
        return  [MWPhoto photoWithURL:[NSURL URLWithString:[headData.photoShowImgArray objectAtIndex:index]]];
    return nil;
    
}


-(void)photoBrowser:(MWPhotoBrowser *)photoBrowser ImgViewOnClickForPhotoAtIndex:(NSUInteger)index{
    
    
    if (index==0&&[StringTool isBlankString:headData.videoUrl]==false&&[UserModal shareUserModel].userType!=UserTypeCustomer) {
        
        CustomMoviePlayerViewController *moviePlayer = [[CustomMoviePlayerViewController alloc] init];
        //将视频地址传过去
        moviePlayer.movieURL = [NSURL URLWithString:headData.videoUrl];
        
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:moviePlayer];
        
        [self.bufNvc presentViewController:nvc animated:YES completion:^{
            
        }];

    }
}




@end
